package yunjiao.javatutorials.designpatterns.singleton;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Monostate 模式
 *
 * @author yangyunjiao
 */
@Slf4j
public class SingletonClassMono {
    private static final String CONNECTION_FILE_WITH_PATH = "dbconnection.properties";

    private static final Properties properties = new Properties();

    static {
        try {
            ClassPathResource resource = new ClassPathResource(CONNECTION_FILE_WITH_PATH);
            properties.load(resource.getInputStream());
        } catch (IOException exp) {
            log.error("", exp);
        }
    }

    public static String getPropertyValue(String propertyKey) {
        if (propertyKey != null && !propertyKey.isEmpty()) {
            return properties.getProperty(propertyKey);
        }
        return null;
    }

    public static Map<String, String> getProperties() {
        Map<String, String> propertyMap = new HashMap<String, String>();
        for (String propertyKey : properties.stringPropertyNames()) {
            propertyMap.put(propertyKey, properties.getProperty(propertyKey));
        }
        return propertyMap;
    }
}
